package cn.com.infosec.ra.util;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class DataPackageImpl implements IDataPackage {
	
	private XStream xs = new XStream();
	@Override
	public void setNode(String name, Class<?> type) throws Exception {
		if (name != null && type != null) {
			this.xs.alias(name, type);
		}

	}

	@Override
	public void setProperty(Class<?> type, String propName) throws Exception {
		if (propName != null && type != null) {
			this.xs.useAttributeFor(type, propName);
		}

	}

	@Override
	public void delCollectionNode(Class<?> type, String collectionNodeName)
			throws Exception {
		if (collectionNodeName != null && type != null) {
			this.xs.addImplicitCollection(type, collectionNodeName);
		}

	}

	@Override
	public String getObjectToXmlString(Object obj) throws Exception {
		String result = "";
		if (obj != null) {
			result = xs.toXML(obj);
		}
		return result;
	}

	@Override
	public Object getObjectFromXmlString(String xmlString, Object obj)
			throws Exception {
		Object result = null;
		if (xmlString != null && obj != null) {
			result = xs.fromXML(xmlString, obj);
		}
		return result;
	}

}
